home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* solar9.c - a simple solar system.
- * Added a reshape handler to reset the viewport and viewing volume.
- * Add depth buffering and back face culling.
- * Add a viewing transformation and control it using mouse input.
- * Enable lighting and give the sun, earth and moon material properties
- *
- * F1 key - print help information
- * Left Mouse Button - change incidence and azimuth angles
- * Middle Mouse Button - change the twist angle based on
- * horizontal mouse movement
- * Right Mouse Button - zoom in and out based on vertical
- * mouse movement
- * <R> Key - reset viewpoint
- * SPACE Key - toggle between solid/wireframe models
- * Escape Key - exit program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <stdio.h>
- #include <math.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid drawScene( GLvoid );
-
- void checkError( char * );
- void printHelp( char * );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
-
- /* Global Variables */
-
- static char *progname;
-
- static GLboolean filledFlag = GL_TRUE;
-
- static enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
- static GLint action;
-
- static GLdouble xStart = 0.0, yStart = 0.0;
-
- static GLfloat sunRadius = 0.7;
- static GLfloat earthRadius = 0.4, earthOrbit = 3.5;
- static GLfloat moonRadius = 0.2, moonOrbit = 1.0;
- static GLfloat near, far, distance, twistAngle, incAngle, azimAngle;
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- /* create a window that is 1/4 the size of the screen,
- * and position it in the middle of the screen.
- */
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitWindowSize( width / 2, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutReshapeFunc( reshape );
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutDisplayFunc( drawScene );
-
- progname = argv[0];
-
- printHelp( progname );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout,
- "\n%s - model some objects\n\n"
- "F1 key - print help information\n"
- "Left Mousebutton - move eye position\n"
- "Middle Mousebutton - change twist angle\n"
- "Right Mousebutton - move up / down to zoom in / out\n"
- "<R> Key - reset viewpoint\n"
- "SPACE key - toggle between solid/wireframe mode\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat maxOrbit;
-
- /* Set up properties for the light */
- GLfloat sunSpecular[] = { 0.0, 0.0, 0.0, 1.0 };
-
- /* set clear color to black */
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
-
- /* enable the depth buffer */
- glEnable( GL_DEPTH_TEST );
-
- /* enable the face culling */
- glEnable( GL_CULL_FACE );
-
- /* Maximum size of all the objects in your scene */
- maxOrbit = earthOrbit + moonOrbit + moonRadius;
-
- /* Set up near and far so that ( far - near ) > maxObjectSize, */
- /* and determine the viewing distance (adjust for zooming) */
- near = 1.0;
- far = near + 8*maxOrbit;
-
- resetView();
-
- /* Set the light properties */
- glLightfv( GL_LIGHT0, GL_SPECULAR, sunSpecular );
-
- /* Turn on the default light */
- glEnable( GL_LIGHT0 );
-
- /* Enable fast material changes for diffuse material */
- glColorMaterial( GL_FRONT, GL_DIFFUSE );
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- /* compute aspect ratio */
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
-
- /* Reset world coordinates first ... */
- glLoadIdentity();
-
- /* Reset the viewing volume based on the new aspect ratio */
- gluPerspective( 45.0, aspect, near, far );
-
- glMatrixMode( GL_MODELVIEW );
- }
-
- void
- checkError( char *label )
- {
- GLenum error;
- while ( (error = glGetError()) != GL_NO_ERROR )
- printf( "%s: %s\n", label, gluErrorString(error) );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case ' ': /* toggle fill mode */
- filledFlag = !filledFlag;
- glutPostRedisplay();
- break;
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_F1: /* Function key #1 */
- /* print help information */
- printHelp( progname );
- break;
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- action = MOVE_EYE;
- break;
- case GLUT_MIDDLE_BUTTON:
- action = TWIST_EYE;
- break;
- case GLUT_RIGHT_BUTTON:
- action = ZOOM;
- break;
- }
-
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- } else {
- action = MOVE_NONE;
- }
-
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- switch (action) {
- case MOVE_EYE:
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- incAngle -= (GLdouble) (y - yStart);
- break;
- case TWIST_EYE:
- /* Adjust the eye twist based on the mouse position */
- twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
- break;
- case ZOOM:
- /* Adjust the eye distance based on the mouse position */
- distance -= (GLdouble) (y - yStart)/10.0;
- break;
- default:
- printf("unknown action %d\n", action);
- }
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
-
- glutPostRedisplay();
- }
-
- void
- resetView( GLvoid )
- {
- distance = near + (far - near) / 2.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static GLfloat year = 45.0, day = -90.0;
-
- static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
- static GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
- static GLfloat gray[] = { 0.4, 0.4, 0.4, 1.0 };
-
- static GLfloat noEmission[] = { 0.0, 0.0, 0.0, 1.0 };
- static GLfloat defaultDiffuse[] = { 0.8, 0.8, 0.8, 1.0 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
-
- /* set up viewing transformation */
- polarView( distance, azimAngle, incAngle, twistAngle );
-
- glEnable( GL_LIGHTING );
- glEnable( GL_COLOR_MATERIAL );
-
- /* draw sun */
-
- /* Give the sun a yellow glow */
- glMaterialfv( GL_FRONT, GL_EMISSION, yellow );
-
- glPushMatrix();
- /* rotate on our own axis */
- glRotatef( 90.0, 1.0, 0.0, 0.0 );
- if (filledFlag)
- glutSolidSphere( sunRadius, 15, 15 );
- else
- glutWireSphere( sunRadius, 15, 15 );
- glPopMatrix();
-
- /* turn emmission off while drawing planets */
- glMaterialfv( GL_FRONT, GL_EMISSION, noEmission );
-
- /* set material for the earth */
- glColor4fv( blue );
-
- glPushMatrix();
-
- /* draw earth */
-
- /* rotate to the right time of year */
- glRotatef( year, 0.0, 1.0, 0.0 );
-
- /* translate out to our orbit about the sun */
- glTranslatef( earthOrbit, 0.0, 0.0 );
- glPushMatrix();
- /* rotate on our own axis */
- glRotatef( 90.0, 1.0, 0.0, 0.0 );
- if (filledFlag)
- glutSolidSphere( earthRadius, 15, 15 );
- else
- glutWireSphere( earthRadius, 15, 15 );
- glPopMatrix();
-
- /* set material for the moon */
- glColor4fv( gray );
-
- /* draw moon */
- glPushMatrix();
- /* rotate to the right time of day */
- glRotatef( day, 0.0, 1.0, 0.0 );
-
- /* translate out to our orbit about the earth */
- glTranslatef( moonOrbit, 0.0, 0.0 );
-
- /* rotate on our axis */
- glRotatef( 90.0, 1.0, 0.0, 0.0 );
- if (filledFlag)
- glutSolidSphere( moonRadius, 15, 15 );
- else
- glutWireSphere( moonRadius, 15, 15 );
- glPopMatrix();
-
- glPopMatrix();
-
- glDisable( GL_COLOR_MATERIAL );
- glDisable( GL_LIGHTING );
-
- glPopMatrix();
-
- checkError( "drawScene" );
-
- glFlush();
- }
-